﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace DemoAstarAlgorithm {

    public enum WindowState { Starting, Active, Ending, Inactive }

    class cMenu {
        private struct MenuItem {
            public string m_sItemText;
            public cMenu m_mnItemLink;

            public MenuItem(string itemText, cMenu itemLink) {
                this.m_sItemText = itemText;
                this.m_mnItemLink = itemLink;
            }
        }

        private TimeSpan m_tsChangeSpan;

        public WindowState windowState;

        private List<MenuItem> m_lItem;

        private int m_iSelectedItem;

        private double m_dChangeProgress;

        public string m_sMenuTitle;

        private Texture2D m_tBackground;

        private Texture2D m_tButtonSelect;

        private Texture2D m_tButtonMenu;

        private List<Rectangle> m_lSelect { get; set; }

        cboom Boom;

        public cMenu(string menuTitle, ContentManager content, GraphicsDevice graphicsDevice) {
            m_lItem = new List<MenuItem>();
            m_tsChangeSpan = TimeSpan.FromMilliseconds(800);
            m_iSelectedItem = 0;
            m_dChangeProgress = 0;
            windowState = WindowState.Inactive;

            m_lSelect = new List<Rectangle>();
            m_lSelect.Add(new Rectangle(0, 0, 80, 80));            //button_starnewgame    0
            m_lSelect.Add(new Rectangle(80, 0, 80, 80));           //button_starnewgame    1
            m_lSelect.Add(new Rectangle(160, 0, 125, 80));         //button_starnewgame    2
            m_lSelect.Add(new Rectangle(0, 80, 218, 60));          //button_endgame         3
            m_lSelect.Add(new Rectangle(220, 80, 217, 60));        //button_endgame         4
            m_lSelect.Add(new Rectangle(0, 0, 320, 60));           //button_menu1           5
            m_lSelect.Add(new Rectangle(0, 60, 320, 60));          //button_menu1           6
            m_lSelect.Add(new Rectangle(0, 120, 320, 60));         //button_menu1           7
            m_lSelect.Add(new Rectangle(0, 180, 320, 60));         //button_menu1           8
            m_lSelect.Add(new Rectangle(320, 0, 320, 60));         //button_menu2           9
            m_lSelect.Add(new Rectangle(320, 60, 320, 60));        //button_menu2           10
            m_lSelect.Add(new Rectangle(320, 120, 320, 60));       //button_menu2           11
            m_lSelect.Add(new Rectangle(320, 180, 320, 60));       //button_menu2           12
            m_lSelect.Add(new Rectangle(0, 250, 640, 240));        //menu giua              13
            Boom = new cboom(content, new Vector2(450, 280), 1);
            this.m_sMenuTitle = menuTitle;
            switch (menuTitle) {
                case "Main Menu":
                    this.m_tBackground = content.Load<Texture2D>(@"images/Background2");
                    this.m_tButtonMenu = content.Load<Texture2D>(@"images/menu");
                    break;
                case "Start a New Game":
                    this.m_tBackground = content.Load<Texture2D>(@"images/SelectMap-new");
                    this.m_tButtonSelect = content.Load<Texture2D>(@"images/button-select");
                    break;
                case "Instruction":
                    this.m_tBackground = content.Load<Texture2D>(@"images/Instruction");
                    break;
                case "Credits":
                    this.m_tBackground = content.Load<Texture2D>(@"images/Credits");
                    break;
                case "EndGame":
                    this.m_tBackground = content.Load<Texture2D>(@"images/Score");
                    this.m_tButtonSelect = content.Load<Texture2D>(@"images/button-select");
                    break;
                default:
                    this.m_tBackground = null;
                    break;
            }
        }

        public void AddMenuItem(string itemText, cMenu itemLink) {
            MenuItem newItem = new MenuItem(itemText, itemLink);
            m_lItem.Add(newItem);
        }

        public void WakeUp() {
            windowState = WindowState.Starting;
        }

        public void Update(double timePassedSinceLastFrame, GameTime gameTime, KeyboardState keyboardState) {
            if ((windowState == WindowState.Starting) || (windowState == WindowState.Ending))
                m_dChangeProgress += timePassedSinceLastFrame / m_tsChangeSpan.TotalMilliseconds;
            Boom.m_iTimeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (Boom.m_iTimeSinceLastFrame > Boom.m_mMillisecondsPerFrame) {
                Boom.m_iTimeSinceLastFrame -= Boom.m_mMillisecondsPerFrame;
                ++Boom.m_pCurrentFrame.X;
                if (Boom.m_pCurrentFrame.X >= Boom.m_pSheetSize.X) {
                    Boom.m_pCurrentFrame.X = 0;
                    ++Boom.m_pCurrentFrame.Y;
                    if (Boom.m_pCurrentFrame.Y >= Boom.m_pSheetSize.Y)
                        Boom.m_pCurrentFrame.Y = 0;
                }
            }
            if (m_dChangeProgress >= 1.0f) {
                m_dChangeProgress = 0.0f;
                if (windowState == WindowState.Starting)
                    windowState = WindowState.Active;
                else if (windowState == WindowState.Ending)
                    windowState = WindowState.Inactive;
            }
        }

        public cMenu ProcessInput(KeyboardState lastKeybState, KeyboardState currentKeybState, ref ManagerMap managerMap) {
            if (lastKeybState.IsKeyUp(Keys.Down) && currentKeybState.IsKeyDown(Keys.Down)) {
                m_iSelectedItem++;
            }
            if (lastKeybState.IsKeyUp(Keys.Up) && currentKeybState.IsKeyDown(Keys.Up)) {
                m_iSelectedItem--;
            }
            if (lastKeybState.IsKeyUp(Keys.Left) && currentKeybState.IsKeyDown(Keys.Left)) {
                m_iSelectedItem--;
            }
            if (lastKeybState.IsKeyUp(Keys.Right) && currentKeybState.IsKeyDown(Keys.Right)) {
                m_iSelectedItem++;
            }

            if (m_iSelectedItem < 0)
                m_iSelectedItem = 0;
            if (m_iSelectedItem >= m_lItem.Count)
                m_iSelectedItem = m_lItem.Count - 1;

            if (m_iSelectedItem == 0 && m_sMenuTitle == "Start a New Game")
                managerMap.map.IDmap = 1;
            if (m_iSelectedItem == 1 && m_sMenuTitle == "Start a New Game")
                managerMap.map.IDmap = 2;

            if (lastKeybState.IsKeyUp(Keys.Enter) && currentKeybState.IsKeyDown(Keys.Enter)) {
                if (m_sMenuTitle == "EndGame") {
                    Game1.g_iScore = 0;
                    if (managerMap.map.IDmap == 1)
                        managerMap.map.IDmap = 2;
                    else
                        managerMap.map.IDmap = 1;
                    managerMap.m_iTimeStatus = 0;
                    managerMap.m_iIDStatus = 1;
                }
                windowState = WindowState.Ending;
                return m_lItem[m_iSelectedItem].m_mnItemLink;
            } else if (lastKeybState.IsKeyDown(Keys.Escape))
                return null;
            else
                return this;
        }

        public void Draw(SpriteBatch spriteBatch, ContentManager content, GameTime gameTime) {
            if (windowState == WindowState.Inactive)
                return;
            float m_fSmoothedProgress = MathHelper.SmoothStep(0, 1, (float)m_dChangeProgress);
            float m_fHorPosition = 300;
            float m_fAlphaValue;
            Color m_clBGColor;
            switch (windowState) {
                case WindowState.Starting:
                    m_fHorPosition -= 200 * (1.0f - (float)m_fSmoothedProgress);
                    m_fAlphaValue = m_fSmoothedProgress;
                    m_clBGColor = new Color(new Vector4(1, 1, 1, m_fAlphaValue));
                    break;
                case WindowState.Ending:
                    m_fHorPosition += 200 * (float)m_fSmoothedProgress;
                    m_fAlphaValue = 1.0f - m_fSmoothedProgress;
                    m_clBGColor = new Color(new Vector4(1, 1, 1, m_fAlphaValue));
                    break;
                default:
                    m_fAlphaValue = 1;
                    m_clBGColor = Color.White;
                    break;
            }
            spriteBatch.Draw(m_tBackground, new Vector2(), null, m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            if (m_sMenuTitle == "Main Menu") {
                spriteBatch.Draw(m_tButtonMenu, new Vector2(100, 20), m_lSelect[13], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                spriteBatch.Draw(m_tButtonMenu, new Vector2(300, 200), m_lSelect[5], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                spriteBatch.Draw(m_tButtonMenu, new Vector2(300, 260), m_lSelect[6], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                spriteBatch.Draw(m_tButtonMenu, new Vector2(300, 320), m_lSelect[7], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                spriteBatch.Draw(m_tButtonMenu, new Vector2(300, 380), m_lSelect[8], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 0.5f);
                switch (m_iSelectedItem) {
                    case 0:
                        spriteBatch.Draw(m_tButtonMenu, new Vector2(290, 200), m_lSelect[9], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        break;
                    case 1:
                        spriteBatch.Draw(m_tButtonMenu, new Vector2(290, 260), m_lSelect[10], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        break;
                    case 2:
                        spriteBatch.Draw(m_tButtonMenu, new Vector2(290, 320), m_lSelect[11], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        break;
                    case 3:
                        spriteBatch.Draw(m_tButtonMenu, new Vector2(290, 380), m_lSelect[12], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                        break;
                    default:
                        break;
                }
            }
            if (m_sMenuTitle == "Start a New Game") {
                if (m_iSelectedItem == 0)
                    spriteBatch.Draw(m_tButtonSelect, new Vector2(140, 475), m_lSelect[0], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                if (m_iSelectedItem == 1)
                    spriteBatch.Draw(m_tButtonSelect, new Vector2(310, 475), m_lSelect[1], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                if (m_iSelectedItem == 2)
                    spriteBatch.Draw(m_tButtonSelect, new Vector2(530, 468), m_lSelect[2], m_clBGColor, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                spriteBatch.Draw(Boom.m_tTexture1, new Vector2(155, 350),
                            new Rectangle(Boom.m_pCurrentFrame.X * Boom.m_pFrameSize.X,
                            Boom.m_pCurrentFrame.Y * Boom.m_pFrameSize.Y,
                            Boom.m_pFrameSize.X, Boom.m_pFrameSize.Y),
                            m_clBGColor, 0, Vector2.Zero,
                            1, SpriteEffects.None, 1);
                spriteBatch.Draw(Boom.m_tTexture2, new Vector2(330, 350),
                            new Rectangle(Boom.m_pCurrentFrame.X * Boom.m_pFrameSize.X,
                            Boom.m_pCurrentFrame.Y * Boom.m_pFrameSize.Y,
                            Boom.m_pFrameSize.X, Boom.m_pFrameSize.Y),
                            m_clBGColor, 0, Vector2.Zero,
                            1, SpriteEffects.None, 1);
            }
            if (m_sMenuTitle == "EndGame") {
                spriteBatch.Draw(m_tBackground, new Vector2(), null, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
                if (m_iSelectedItem == 0)
                    spriteBatch.Draw(m_tButtonSelect, new Vector2(503, 207), m_lSelect[3], Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
                if (m_iSelectedItem == 1)
                    spriteBatch.Draw(m_tButtonSelect, new Vector2(503, 316), m_lSelect[4], Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
            }
        }
    }
}
